package com.bestescortbedsystem.config;

import com.bestescortbedsystem.shiro.MyRealm;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: ShiroConfig
 * Package: com.bestescortbedsystem.config
 *
 * @author: guanzepeng
 * @Date: 2021/2/17 10:54 星期三
 * @Description: Shiro配置类
 */
@Configuration
@Slf4j
public class ShiroConfig {

	/**
	 * 负责拦截所有请求
	 * @return
	 */
	@Bean(name = "shiroFilterFactoryBean")
	public ShiroFilterFactoryBean getShiroFilterFactoryBean (DefaultWebSecurityManager defaultWebSecurityManager){
		ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
		//给filter设置安全管理器
		shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);
		//配置系统受限资源
		//配置系统公共资源
		Map<String, String> map = new HashMap<>();
//		map.put()
		shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
		return shiroFilterFactoryBean;
	}

	/**
	 * 创建安全管理器
	 * @return
	 */
	@Bean
	public DefaultWebSecurityManager getDefaultWebSecurityManager(Realm realm){
		DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
		//给安全管理器设置realm
		defaultWebSecurityManager.setRealm(realm);
		return defaultWebSecurityManager;
	}

	/**
	 * 自定义realm
	 * @return
	 */
	@Bean
	public Realm getRealm(){
		MyRealm myRealm = new MyRealm();
		//修改凭证校验匹配器
		HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
		//设置密码加密算法为MD5
		hashedCredentialsMatcher.setHashAlgorithmName("md5");
		//设置散列次数
		hashedCredentialsMatcher.setHashIterations(1024);
		//将修改后的数据放入自定义realm
		myRealm.setCredentialsMatcher(hashedCredentialsMatcher);
		return myRealm;
	}

//	/**
//	 * cookie对象;
//	 * rememberMeCookie()方法是设置Cookie的生成模版，比如cookie的name，cookie的有效时间等等。
//	 * @return
//	 */
//	@Bean
//	public SimpleCookie rememberMeCookie(){
//		//这个参数是cookie的名称，对应前端的checkbox的name = rememberMe
//		SimpleCookie simpleCookie = new SimpleCookie("rememberMe");
//		//<!-- 记住我cookie生效时间30天 ,单位秒;-->
//		simpleCookie.setMaxAge(259200);
//		return simpleCookie;
//	}


}
